cmake_minimum_required (VERSION 2.6)
# We are only interested in finding jni.h: we do not care about extended JVM
# functionality or the AWT library.
# 查找OpenMP
find_package(OpenMP)
# CPU体系对应的SIMD指令集名称
set(    arm_SIMD neon CACHE INTERNAL "SIMD intruction set for arm architecture")
set(   mips_SIMD msa  CACHE INTERNAL "SIMD intruction set for mips architecture")
set(    x86_SIMD avx2 CACHE INTERNAL "SIMD intruction set for x86 architecture")
set( x86_64_SIMD avx2 CACHE INTERNAL "SIMD intruction set for x86_64 architecture")
set(ARCH_TYPE ${CMAKE_SYSTEM_PROCESSOR})
if(CMAKE_GENERATOR_PLATFORM)
	set(ARCH_TYPE ${CMAKE_GENERATOR_PLATFORM})
endif()
if( ARCH_TYPE MATCHES "x86_64|x64|AMD64|Win64")
	set(ARCH_TYPE  "x86_64")
elseif(ARCH_TYPE MATCHES  "Win32|x86")
	set(ARCH_TYPE  "x86")
elseif(ARCH_TYPE MATCHES  "^armv7|aarch64")
	set(ARCH_TYPE  "arm")
endif()
if(NOT ARCH_SIMD)
	set(ARCH_SIMD ${${ARCH_TYPE}_SIMD})
endif()
if(NOT ARCH_SIMD STREQUAL "${${ARCH_TYPE}_SIMD}")
	set(JIU_LIBNAME "${JIU_LIBNAME}_${ARCH_SIMD}")
endif()
message(STATUS ARCH_SIMD=${ARCH_SIMD})
# 当前文件夹下所有c/cpp文件
aux_source_directory(${CMAKE_CURRENT_LIST_DIR} _SRC_SOURCES)
set(_public_headers)
list(APPEND _public_headers ${CMAKE_CURRENT_LIST_DIR}/jimgutil.h)
add_library(${JIU_LIBNAME} SHARED ${_SRC_SOURCES})

# 使用静态库
use_static_library(${JIU_LIBNAME})
# android交叉编译添加log库用于调试
target_link_libraries(${JIU_LIBNAME} $<$<PLATFORM_ID:Android>:log>)

target_include_directories (${JIU_LIBNAME}	PRIVATE ${DEPENDENT_SOURCE_DIR}
	INTERFACE "$<INSTALL_INTERFACE:include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>"
)
#############设置动态库后缀###################
set_target_properties(${JIU_LIBNAME} 
	PROPERTIES 
	DEBUG_POSTFIX _d
	PUBLIC_HEADER "${_public_headers}"
	INTERFACE_POSITION_INDEPENDENT_CODE ON)
#target_compile_options(${JIU_LIBNAME} PRIVATE $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:JIMGUTIL_EXPORTS>)
add_compile_definitions($<BUILD_INTERFACE:JIMGUTIL_EXPORTS>)
# 设置SIMD指令优化选项
# 开启OpenMP
# NDK 交叉编译时使用工具链自动设置
target_compile_options(${JIU_LIBNAME} PRIVATE 
	$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>:-arch:$<UPPER_CASE:${ARCH_SIMD}>> 
	$<$<AND:$<CXX_COMPILER_ID:GNU>,$<CONFIG:Release>,$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Android>>,$<NOT:$<STREQUAL:$<LOWER_CASE:${ARCH_TYPE}>,arm>>>:-fopt-info -m$<LOWER_CASE:${ARCH_SIMD}>> 		
	$<$<AND:$<CXX_COMPILER_ID:GNU>,$<CONFIG:Release>,$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Android>>,$<STREQUAL:$<LOWER_CASE:${ARCH_TYPE}>,arm>>: -ftree-vectorize -mfpu=$<LOWER_CASE:${ARCH_SIMD}>>
	$<$<AND:$<CXX_COMPILER_ID:Clang>,$<CONFIG:Release>,$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Android>>,$<NOT:$<STREQUAL:$<LOWER_CASE:${ARCH_TYPE}>,arm>>>:-fopt-info -m$<LOWER_CASE:${ARCH_SIMD}>> 		
	$<$<AND:$<CXX_COMPILER_ID:Clang>,$<CONFIG:Release>,$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Android>>,$<STREQUAL:$<LOWER_CASE:${ARCH_TYPE}>,arm>>: -ftree-vectorize -mfpu=$<LOWER_CASE:${ARCH_SIMD}>>
	$<$<BOOL:${OpenMP_FOUND}>:${OpenMP_CXX_FLAGS}>
	)
#######Windows 下DLL 文件名前缀为空 #######
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
	set_property(TARGET ${JIU_LIBNAME} PROPERTY  PREFIX "")
endif(CMAKE_SYSTEM_NAME MATCHES "Windows")
########安装脚本##########
install(TARGETS ${JIU_LIBNAME} 
        RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
		LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
		PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/${JIU_LIBNAME}
        )
if(MSVC)
	# 安装pdb文件
	install(FILES $<TARGET_PDB_FILE:${JIU_LIBNAME}> DESTINATION bin OPTIONAL)
endif(MSVC)
unset(_public_headers)